Welcome back.
I hope the quiz went well for you.
I hope you rocked it.
And now we're going to work on another little project, or practicing with our better twin module by
creating a door that has a proximity prompt.
And when you interact with it, the door opens, and when you interact with it again, the door closes.
So what I've done is I've created a folder in the workspace called doors, and I've placed this sliding
door inside of it.
You're going to be able to grab this sliding door as a resource attached to this lecture.
So go to the resources section for this lecture.
Grab this model and insert it into your game and place it in a folder called doors.
Because what you're going to do is I want you to go through all of the doors in the map and basically
listen to all their proximity prompts and tween them when the prompts are interacted with.
So I should be able to interact.
The prompt should disappear.
the door should tween and when it's done tweening the prompt should reappear.
And then when you interact with the prompt, it should close the door and the prompt should reappear
again.
So I would like for you to take a few minutes of your time to go ahead and work on this project, and
if you're not able to do it on your own quite yet, don't worry, I'm going to show you my solution
next.
But please take a few minutes to attempt to try to create this system.
All right.
Hopefully you got your system working.
If you didn't, we're going to walk through the project together so you can see how I would approach
it.
So we need to control all of the doors on our game in this folder called doors.
So let's create a server script in Server Script service.
And this script is going to be responsible for handling all the doors and listening for when their proximity
prompts are triggered.
So we can call this script something like our door service.
Inside of here, we're going to need a reference to replicated storage so we can get our better tween
module.
So we'll do game get service replicated storage.
And then let's go ahead and require our better tween module.
So we're going to do require replicated storage dot better tween.
Now what we need to do is we basically need to loop through all of the doors inside of the doors folder,
so we could do for every single door in Ipairs we're going to do workspace dot doors Getchildren.
So we'll grab all of the doors in our game, and we need to listen to each one of these doors.
Proximity prompts.
So all the doors on our game, the proximity prompt is located in the frame.
So what we can go ahead and do is we can do door dot frame, dot proximity prompt dot triggered.
And we need to connect a function to this event.
And we need to basically tween the door.
Now to save a little bit on memory, I'm going to create a function outside of my for loop.
I'll just call it on prompt triggered.
That way I can call this function from within this lambda function and pass some arguments to it.
That way if there's, let's say hundreds or thousands of doors in my game, they can all refer or call
this one function.
And I don't need to create thousands of functions for every single door in my game.
Through each iteration of this loop, I'm instead going to refer to one function I create out here.
So what we need to do is we need to check to see what the current key frame of the door is.
If the door is key, frame is at its original key frame.
That means we need to open the door.
Otherwise, if the door is not at its original key frame, we need to close the door.
So inside of our for loop, let's create a variable called original key frame and we need to tween the
door part inside of our sliding door, as that is what all of our parts are welded to.
They're all welded to this door part.
So the original keyframe is going to be my door dot door dot keyframe.
And then when my prompt is triggered, let's just go ahead and pass our door to this function as well
as the original keyframe.
And then this function of course is going to get those two arguments.
So it's going to be the door and the original keyframe.
And when the prompt is triggered, what we need to do is we need to disable the prompt so we can do
door dot frame dot proximity prompt dot enabled.
We'll set that equal to false.
Then we need to create a tween info for the door.
So tween info is equal to tween info dot new.
You could have designed your tween info however you want and pass whatever you want, but for this demonstration,
I'm just going to have a tween of two seconds and leave the rest at the defaults.
And then what we need to do is we need to have an if statement to check to see if the door's door part
dot c frame is equal to the original C frame.
If it is, then we need to open the door.
Otherwise, if it's not equal to the original C frame, we need to close the door.
So this is where we get to use our better tween module.
We're going to call the tween function.
And we need to pass our instance which is going to be door dot door the tween info which we just created.
And then the goal.
So the goal will make a table.
Here we need to tween the C frame of this part.
And that C frame is going to have to be the original C frame, offset by some kind of value in a particular
direction on, let's say I think the x axis.
Because if we look at our door here, right, this is the door on the x axis or its local x axis.
So we need to tween the door basically that way on its local axis.
Now in your solution you might have used a vector three here and would have been like, okay, let me
just tween it by um, some kind of amount on the x axis.
The proper way to do this would be to get the size of the door on its, uh, x axis, so I could do.
Door.
Dot door.
Dot size dot x.
So let's just do that.
And then otherwise if the door is not at the original C frame and we need to close it, let's just copy
this.
But this time let's just tween it to the original keyframe of the door.
And then finally, we need to re-enable the prompt after the tween is finished.
And if you remember, we are returning a number from this function that represents how long the tween
lasts.
So let's make a variable out here.
Let's call it tween duration.
And I'm not going to initialize it with any value because we're going to set it here.
And we're also going to set it down here.
And then we're going to yield for that duration.
And after that duration is up we can go ahead and set the proximity prompt to be enabled again.
Okay.
So let's go ahead and test to see if our solution works.
I'm going to play here with my player's character.
And let's interact with the sliding door and look at that.
It opens and it closes.
Look at that.
It's working good.
But I want to kind of demonstrate a little bit of an issue to you.
What if I rotate my door at an angle?
Let's say my door is sloped at like 45 degrees, right.
And I go and play the game and I want to open my door.
Uh oh.
What the heck?
My door is not supposed to be doing that.
Why is it moving like that?
Well, the reason why the door is moving like this is because we are offsetting the C frame of this
door on the global x axis.
So the x axis in the game globally is this direction, which is why the door moves in that direction.
Instead, what we need to do is we need to offset the C frame of our door by another C frame, and by
doing so, instead of the door moving according to the global axes in the game, the door will move
according to the local axes of our part.
And I'll demonstrate that to you.
Let me go back to my door here and let me select my door and let me go and select the move tool.
So as you can see, these red arrows represent the local axes of my door.
Now if I press control and l.
Now I have swapped the move tool to be in the global axes mode.
So these arrows now represent the global axes in the game.
So as you can see, it makes sense why our door moved this way.
But instead we want to move it according to its local axes.
So the door moves this way.
So to do that, let's go back to our door service.
And instead of offsetting our keyframe by a vector three, we simply just need to offset it by a new
keyframe just like that.
And then if we go and play test the game and we go and interact with the door, oh no, we got an error.
What?
Invalid argument number two.
Vector three expected got See?
Frame.
What is going on here?
Well, the issue is we actually need to replace this addition symbol with a multiplication symbol.
Now we are not multiplying the c frame.
We are simply just adding this other c frame to it.
The reason why we have to swap it from an addition symbol to a multiplication symbol is.
That's just because I don't know.
That's what the developers decided when they were making this game engine like 16, 18, 19 years ago.
This is something you're just going to have to remember.
If you want to offset a keyframe by another keyframe, you have to use a multiplication symbol.
If you want to offset a keyframe by a vector three, you have to use an addition symbol.
There really isn't, I don't think any good idea or reason for this.
It's just this is what the developers chose when they created roadblocks like 19 years ago.
So you're just going to have to remember that.
Sorry, but now with a multiplication symbol, if we go and play the game and we interact with the sliding
door, there we go.
Now it works and the door moves according to the local axes.
Alrighty, I hope you enjoyed this practice challenge and I also hope you learned a lot.
Thank you for watching and I will see you in the next lecture.